"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = require("vue");

var _menu = _interopRequireWildcard(require("../../menu"));

var _MentionsContext = _interopRequireDefault(require("./MentionsContext"));

var _spin = _interopRequireDefault(require("../../spin"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function noop() {}

var _default2 = (0, _vue.defineComponent)({
  name: 'DropdownMenu',
  props: {
    prefixCls: String,
    options: {
      type: Array,
      default: function _default() {
        return [];
      }
    }
  },
  slots: ['notFoundContent', 'option'],
  setup: function setup(props, _ref) {
    var slots = _ref.slots;

    var _inject = (0, _vue.inject)(_MentionsContext.default, {
      activeIndex: (0, _vue.ref)(),
      loading: (0, _vue.ref)(false)
    }),
        activeIndex = _inject.activeIndex,
        setActiveIndex = _inject.setActiveIndex,
        selectOption = _inject.selectOption,
        _inject$onFocus = _inject.onFocus,
        onFocus = _inject$onFocus === void 0 ? noop : _inject$onFocus,
        _inject$onBlur = _inject.onBlur,
        onBlur = _inject$onBlur === void 0 ? noop : _inject$onBlur,
        loading = _inject.loading;

    return function () {
      var _a;

      var prefixCls = props.prefixCls,
          options = props.options;
      var activeOption = options[activeIndex.value] || {};
      return (0, _vue.createVNode)(_menu.default, {
        "prefixCls": "".concat(prefixCls, "-menu"),
        "activeKey": activeOption.value,
        "onSelect": function onSelect(_ref2) {
          var key = _ref2.key;
          var option = options.find(function (_ref3) {
            var value = _ref3.value;
            return value === key;
          });
          selectOption(option);
        },
        "onBlur": onBlur,
        "onFocus": onFocus
      }, {
        default: function _default() {
          return [!loading.value && options.map(function (option, index) {
            var _a, _b;

            var value = option.value,
                disabled = option.disabled,
                _option$label = option.label,
                label = _option$label === void 0 ? option.value : _option$label;
            return (0, _vue.createVNode)(_menu.Item, {
              "key": value,
              "disabled": disabled,
              "onMouseenter": function onMouseenter() {
                setActiveIndex(index);
              }
            }, {
              default: function _default() {
                return [(_b = (_a = slots.option) === null || _a === void 0 ? void 0 : _a.call(slots, option)) !== null && _b !== void 0 ? _b : typeof label === 'function' ? label({
                  value: value,
                  disabled: disabled
                }) : label];
              }
            });
          }), !loading.value && options.length === 0 ? (0, _vue.createVNode)(_menu.Item, {
            "key": "notFoundContent",
            "disabled": true
          }, {
            default: function _default() {
              return [(_a = slots.notFoundContent) === null || _a === void 0 ? void 0 : _a.call(slots)];
            }
          }) : null, loading.value && (0, _vue.createVNode)(_menu.Item, {
            "key": "loading",
            "disabled": true
          }, {
            default: function _default() {
              return [(0, _vue.createVNode)(_spin.default, {
                "size": "small"
              }, null)];
            }
          })];
        }
      });
    };
  }
});

exports.default = _default2;